package com.systemsjr.mpf.expense;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.systemsjr.mpf.IntentHandler;
import com.systemsjr.mpf.MpfActivity;
import com.systemsjr.mpf.R;
import com.systemsjr.mpf.account.Account;
import com.systemsjr.mpf.account.ManageAccountActivity;

public class ExpenseTypeList extends ListActivity {

	private SimpleCursorAdapter adapter;
	private ListView expeseTypeView;
	private Cursor c;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expense_type_list);

		String[] from = { ExpenseType.CODE, ExpenseType.EXPENSE_TYPE };
		ExpenseType expenseType = new ExpenseType();

		c = getContentResolver().query(ExpenseType.CONTENT_URI, ExpenseType.PROJECTIONS,
				null, null, null);
		String[] selectionArgs = { "%", "%" };

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.size() > 0) {
			if (bundle.getString(ExpenseType.CODE) != null) {
				expenseType.setCode(bundle.getString(ExpenseType.CODE));
				
			}

			if (bundle.getString(ExpenseType.EXPENSE_TYPE) != null) {
				expenseType.setExpenseType(bundle
						.getString(ExpenseType.EXPENSE_TYPE));
				
			}

			selectionArgs = new String[] {
					expenseType.getCode() == null
							|| expenseType.getCode().length() == 0 ? "%" : "%"
							+ expenseType.getCode() + "%",
					expenseType.getExpenseType() == null
							|| expenseType.getExpenseType().length() == 0 ? "%"
							: "%" + expenseType.getExpenseType() + "%" };

		}

		String selection = ExpenseType.CODE + " LIKE ? AND " + ExpenseType.EXPENSE_TYPE + " LIKE ?";

		c = getContentResolver().query(ExpenseType.CONTENT_URI, ExpenseType.PROJECTIONS,
				selection, selectionArgs, null);
		//System.out.println("Found " + c.getCount());
		startManagingCursor(c);

		int[] to = { R.id.type_code_entry, R.id.exp_type_entry };

		adapter = new SimpleCursorAdapter(this, R.layout.expense_type_list_entry, c,
				from, to, 0);
		expeseTypeView = getListView();

		expeseTypeView.setAdapter(adapter);

		Button expenseTypeButton = (Button) findViewById(R.id.addExpenseTypeButton);
		expenseTypeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(ExpenseTypeList.this,
						ManageExpenseTypeActivity.class);
				startActivity(intent);
			}
		});

		Button expenseButton = (Button) findViewById(R.id.addExpenseButton);
		expenseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(ExpenseTypeList.this,
						ManageExpenseActivity.class);
				startActivity(intent);

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.expense_type_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = IntentHandler.handleOnOptionsItemSelected(this, item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(ExpenseTypeList.this, ManageExpenseTypeActivity.class);
		
		if (adapter != null) {
			Cursor c = (Cursor) adapter.getItem(position);
			intent.putExtra(ExpenseType.CODE, c.getString(c.getColumnIndex(ExpenseType.CODE)));
			intent.putExtra(ExpenseType.EXPENSE_TYPE, c.getString(c.getColumnIndex(ExpenseType.EXPENSE_TYPE)));
			intent.putExtra(ExpenseType.ID, c.getLong(c.getColumnIndex(ExpenseType.ID)));
		}
		
		startActivityForResult(intent, 1);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//c.requery();
		adapter.notifyDataSetChanged();
		expeseTypeView.invalidateViews();
	}
}
